package com.nuanshui.heatedloan.credit.msg;

import com.alibaba.dubbo.config.annotation.Reference;
import com.nuanshui.heatedloan.constant.GlobalParam;
import com.nuanshui.heatedloan.msgcenter.constant.SmsMsgBizType;
import com.nuanshui.heatedloan.msgcenter.integration.AppPushMsgBody;
import com.nuanshui.heatedloan.msgcenter.integration.CreditPushMsgBody;
import com.nuanshui.heatedloan.msgcenter.integration.MsgTools;
import com.nuanshui.heatedloan.msgcenter.model.AliSmsNotificationCodeTemp;
import com.nuanshui.heatedloan.msgcenter.model.MsgBizType;
import com.nuanshui.heatedloan.msgcenter.model.SmsPushMsgBody;
import com.nuanshui.heatedloan.userinfo.facade.UserTokenService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

@Component
public class CreditPushMsgService implements InitializingBean {

    private Logger logger = LoggerFactory.getLogger(CreditPushMsgService.class);

    @Reference(url= GlobalParam.USER_DUBBO_SERVER,timeout=GlobalParam.DUBBO_SERVER_TIME_OUT,version=GlobalParam.USER_DUBBO_SERVER_VERSION)
    private UserTokenService userTokenService;

    @Autowired
    private MsgTools msgTools;

    private void handlePassAuthType(CreditPushMsgBody creditPushMsgBody)
    {
        SmsPushMsgBody model=new SmsPushMsgBody();
        model.addDstUserId(creditPushMsgBody.getDstUserId());
        int loadAmout = creditPushMsgBody.getAmount()/100;
        logger.info("***************认证额度结果loadAmout[ "+loadAmout+" ]");
        if(loadAmout>0){
            logger.info("***************认证通过短信、推送开始了*****************");
            model.setBizType(SmsMsgBizType.SINGLELOAN_LOADAMOUNT_MESSAGE);
            model.setTemplateCode(AliSmsNotificationCodeTemp.SINGLELOAN_LOADAMOUNT_MESSAGE.gettempCode());
            model.putParam("loadAmout",loadAmout);
            creditPushMsgBody.setMsgTitle(CreditPushConstant.PASS_AUTH_TITLE);
            creditPushMsgBody.setMsgContent(CreditPushConstant.PASS_AUTH_CONTENT_START+(loadAmout)+ CreditPushConstant.PASS_AUTH_CONTENT_END);
        }
        if(loadAmout <= 0) {
            logger.info("***************认证未通过开始了*****************");
            model.setBizType(SmsMsgBizType.SINGLELOAN_MESSAGE);
            model.setTemplateCode(AliSmsNotificationCodeTemp.SINGLELOAN_MESSAGE.gettempCode());
            creditPushMsgBody.setMsgTitle(CreditPushConstant.NOT_PASS_AUTH_TITLE);
            creditPushMsgBody.setMsgContent(CreditPushConstant.NOT_PASS_AUTH_CONTENT);
        }
        //审核消息
        creditPushMsgBody.setMessageType(MsgBizType.AUDIT.getType());
        creditPushMsgBody.setBizType(MsgBizType.AUDIT.getTypeName());
        msgTools.sendComposeMsg(creditPushMsgBody,model);
    }

    private void handleNotPassAuthType(CreditPushMsgBody creditPushMsgBody) {
        logger.info("***************认证未通过开始了*****************");
        SmsPushMsgBody model=new SmsPushMsgBody();
        model.addDstUserId(creditPushMsgBody.getDstUserId());
        model.setBizType(SmsMsgBizType.SINGLELOAN_MESSAGE);
        model.setTemplateCode(AliSmsNotificationCodeTemp.SINGLELOAN_MESSAGE.gettempCode());

        creditPushMsgBody.setMsgTitle(CreditPushConstant.NOT_PASS_AUTH_TITLE);
        creditPushMsgBody.setMsgContent(CreditPushConstant.NOT_PASS_AUTH_CONTENT);
        creditPushMsgBody.setMessageType(MsgBizType.AUDIT.getType());//审核消息
        creditPushMsgBody.setBizType(MsgBizType.AUDIT.getTypeName());
        msgTools.sendComposeMsg(creditPushMsgBody,model);
    }


    private void handlePassAuditType(CreditPushMsgBody creditPushMsgBody) {
        logger.info("***************贷款申请通过了*****************");
        SmsPushMsgBody model=new SmsPushMsgBody();
        model.addDstUserId(creditPushMsgBody.getDstUserId());
        model.setBizType(SmsMsgBizType.AUDIT_PASS_MESSAGE);
        model.setTemplateCode(AliSmsNotificationCodeTemp.AUDIT_PASS_MESSAGE.gettempCode());

        creditPushMsgBody.setMsgTitle(CreditPushConstant.PASS_AUDIT_TITLE);
        creditPushMsgBody.setMsgContent(CreditPushConstant.PASS_AUDIT_CONTENT);
        creditPushMsgBody.setMessageType(MsgBizType.AUDIT.getType());//审核消息
        creditPushMsgBody.setBizType(MsgBizType.AUDIT.getTypeName());

        msgTools.sendComposeMsg(creditPushMsgBody,model);
    }

    private void handleNotPassAuditType(CreditPushMsgBody creditPushMsgBody) {
        logger.info("***************贷款申请未通过*****************");
        SmsPushMsgBody model=new SmsPushMsgBody();
        model.addDstUserId(creditPushMsgBody.getDstUserId());
        model.setBizType(SmsMsgBizType.AUDIT_NOT_PASS_MESSAGE);
        model.setTemplateCode(AliSmsNotificationCodeTemp.AUDIT_NOT_PASS_MESSAGE.gettempCode());

        creditPushMsgBody.setMsgTitle(CreditPushConstant.NOT_PASS_AUDIT_TITLE);
        creditPushMsgBody.setMsgContent(CreditPushConstant.NOT_PASS_AUDIT_CONTENT);
        creditPushMsgBody.setMessageType(MsgBizType.AUDIT.getType());//审核消息

        msgTools.sendComposeMsg(creditPushMsgBody,model);
    }

    private void handleLendPassType(CreditPushMsgBody creditPushMsgBody) {
        int loadAmout = creditPushMsgBody.getAmount()/100;
        String orderNo =creditPushMsgBody.getOrderNo();
        String shouldReturnTime =creditPushMsgBody.getShouldReturnTime();
        logger.info("***************贷款申请通过结果Amout[ "+loadAmout+" ]");
        SmsPushMsgBody model=new SmsPushMsgBody();
        model.addDstUserId(creditPushMsgBody.getDstUserId());
        model.setBizType(SmsMsgBizType.LEND_SUCCESS_MESSAGE);
        model.setTemplateCode(AliSmsNotificationCodeTemp.LEND_SUCCESS_MESSAGE.gettempCode());
        model.putParam("loadAmout",loadAmout);
        model.putParam("orderNo",orderNo);
        model.putParam("shouldReturnTime",shouldReturnTime);

        creditPushMsgBody.setMsgTitle(CreditPushConstant.LEND_PASS_TITLE);
        creditPushMsgBody.setMsgContent(CreditPushConstant.LEND_PASS_CONTENT);
        creditPushMsgBody.setMessageType(MsgBizType.AUDIT.getType());//审核消息

        msgTools.sendComposeMsg(creditPushMsgBody,model);
    }

    private void handleNotLendPassType(CreditPushMsgBody creditPushMsgBody) {
        String orderNo =creditPushMsgBody.getOrderNo();
        logger.info("***************贷款申请未通过，订单号orderNo[ "+orderNo+" ]");
        SmsPushMsgBody model=new SmsPushMsgBody();
        model.addDstUserId(creditPushMsgBody.getDstUserId());
        model.setBizType(SmsMsgBizType.LEND_FAIL_MESSAGE);
        model.setTemplateCode(AliSmsNotificationCodeTemp.LEND_FAIL_MESSAGE.gettempCode());
        model.putParam("orderNo",orderNo);

        creditPushMsgBody.setMsgTitle(CreditPushConstant.NOT_LEND_PASS_TITLE);
        creditPushMsgBody.setMsgContent(CreditPushConstant.NOT_LEND_PASS_CONTENT);
        creditPushMsgBody.setMessageType(MsgBizType.AUDIT.getType());//审核消息

        msgTools.sendComposeMsg(creditPushMsgBody,model);
    }

    private void handleNotOverduRemindType(CreditPushMsgBody creditPushMsgBody) {
        String orderNo =creditPushMsgBody.getOrderNo();
        int loadAmount = creditPushMsgBody.getAmount()/100;
        logger.info("***************未逾期，订单号orderNo[ "+orderNo+" ]，需要金额loadAmout[ "+loadAmount+" ]");
        SmsPushMsgBody model=new SmsPushMsgBody();
        model.addDstUserId(creditPushMsgBody.getDstUserId());
        int remainDay= creditPushMsgBody.getRemainDay();
        if(remainDay == 2){
            model.setBizType(SmsMsgBizType.TWO_DAY_BEFORE_REPAYMENT);
            model.setTemplateCode(AliSmsNotificationCodeTemp.TWO_DAY_BEFORE_REPAYMENT.gettempCode());
        }else if(remainDay == 1){
            model.setBizType(SmsMsgBizType.DAY_BEFORE_REPAYMENT);
            model.setTemplateCode(AliSmsNotificationCodeTemp.DAY_BEFORE_REPAYMENT.gettempCode());
        }else  if (remainDay == 0){
            model.setBizType(SmsMsgBizType.DAY_NOW_REPAYMENT);
            model.setTemplateCode(AliSmsNotificationCodeTemp.DAY_NOW_REPAYMENT.gettempCode());
        }
        model.putParam("orderNo",orderNo);
        model.putParam("loadAmount",loadAmount);
        if(remainDay == 0 ||remainDay == 1 || remainDay == 2){
            creditPushMsgBody.setMsgTitle(CreditPushConstant.NOT_REMIND_TITLE);
            creditPushMsgBody.setMsgContent(CreditPushConstant.NOT_REMIND_CONTENT_START+ creditPushMsgBody.getOverduDay()+ CreditPushConstant.NOT_REMIND_CONTENT_END);
            creditPushMsgBody.setMessageType(MsgBizType.REPAYMENT.getType());//还款消息
        }
        msgTools.sendComposeMsg(creditPushMsgBody,model);
    }

    private void handleOverduRemindType(CreditPushMsgBody creditPushMsgBody) {
        int loadAmount = creditPushMsgBody.getAmount()/100;
        logger.info("***************逾期，需还金额loadAmout[ "+loadAmount+" ]");
        SmsPushMsgBody model=new SmsPushMsgBody();
        model.addDstUserId(creditPushMsgBody.getDstUserId());
        int overduDay = creditPushMsgBody.getOverduDay();
        if(overduDay == 1 || overduDay ==2){
            model.setBizType(SmsMsgBizType.OVERDUE_DAY_REPAYMENT);
            model.setTemplateCode(AliSmsNotificationCodeTemp.OVERDUE_DAY_REPAYMENT.gettempCode());
            model.putParam("overDueDay",overduDay);
            model.putParam("loadAmount",loadAmount);
        }else  if (overduDay >2 && overduDay <11){
            model.setBizType(SmsMsgBizType.OVERDUE_DAYS_REPAYMENT);
            model.setTemplateCode(AliSmsNotificationCodeTemp.OVERDUE_DAYS_REPAYMENT.gettempCode());
            model.putParam("overDueDay",overduDay);
        }
        if(overduDay<11){
            creditPushMsgBody.setMsgTitle(CreditPushConstant.REMIND_TITLE);
            creditPushMsgBody.setMsgContent(CreditPushConstant.REMIND_CONTENT_START+creditPushMsgBody.getOverduDay()+CreditPushConstant.REMIND_CONTENT_END);
            creditPushMsgBody.setMessageType(MsgBizType.REPAYMENT.getType());//还款消息
        }
        msgTools.sendComposeMsg(creditPushMsgBody,model);
    }

    private void handleRepayType(CreditPushMsgBody creditPushMsgBody) {
        logger.info("***************还款成功提额******************");
        creditPushMsgBody.setMsgTitle(CreditPushConstant.REPAY_TITLE);
        creditPushMsgBody.setMsgContent(CreditPushConstant.REPAY_CONTEN_START+(creditPushMsgBody.getAmount()/100)+ CreditPushConstant.REPAY_CONTEN_END);
        creditPushMsgBody.setMessageType(MsgBizType.REPAYMENT.getType());//还款消息

        msgTools.sendComposeMsg(creditPushMsgBody);
    }

    private void handleRemindRepayType(CreditPushMsgBody creditPushMsgBody) {
        logger.info("***************还款成功降额******************");
        creditPushMsgBody.setMsgTitle(CreditPushConstant.REMINDREPAY_TITLE);
        creditPushMsgBody.setMsgContent(CreditPushConstant.REMIND_REPAY_CONTENT_START+(creditPushMsgBody.getAmount()/100)+ CreditPushConstant.REMIND_REPAY_CONTENT_END);
        creditPushMsgBody.setMessageType(MsgBizType.REPAYMENT.getType());//还款消息

        msgTools.sendComposeMsg(creditPushMsgBody);
    }

    private void handleRepaySuccessType(CreditPushMsgBody creditPushMsgBody){
        logger.info("***************还款成功******************");
        int loadAmount = creditPushMsgBody.getAmount()/100;
        SmsPushMsgBody model=new SmsPushMsgBody();
        model.addDstUserId(creditPushMsgBody.getDstUserId());
        model.setBizType(SmsMsgBizType.REPAYMENT_SUCCESS);
        model.setTemplateCode(AliSmsNotificationCodeTemp.REPAYMENT_SUCCESS.gettempCode());
        model.putParam("loadAmount",loadAmount);

        creditPushMsgBody.setMsgTitle(CreditPushConstant.REPAY_SUCCESS_TITLE);
        creditPushMsgBody.setMsgContent(CreditPushConstant.REPAY_SUCCESS_CONTENT);
        creditPushMsgBody.setMessageType(MsgBizType.REPAYMENT.getType());//还款消息

        msgTools.sendComposeMsg(creditPushMsgBody);
    }

    private void handleRepayFailType(CreditPushMsgBody creditPushMsgBody) {
        logger.info("***************还款失败******************");
        int loadAmount = creditPushMsgBody.getAmount()/100;
        SmsPushMsgBody model=new SmsPushMsgBody();
        model.addDstUserId(creditPushMsgBody.getDstUserId());
        model.setBizType(SmsMsgBizType.REPAYMENT_FAIL);
        model.setTemplateCode(AliSmsNotificationCodeTemp.REPAYMENT_FAIL.gettempCode());
        model.putParam("loadAmount",loadAmount);

        creditPushMsgBody.setMsgTitle(CreditPushConstant.REPAY_FAIL_TITLE);
        creditPushMsgBody.setMsgContent(CreditPushConstant.REPAY_FAIL_CONTENT);
        creditPushMsgBody.setMessageType(MsgBizType.REPAYMENT.getType());//还款消息

        msgTools.sendComposeMsg(creditPushMsgBody);
    }

    @Transactional(readOnly=false, value= com.nuanshui.framework.consts.TransactionManagerType.CREDIT_TRANSACTION_MANAGER)
    public void dealPush(CreditPushMsgBody creditPushMsgBody) {
        if(creditPushMsgBody != null){

            creditPushMsgBody.setFlag(AppPushMsgBody.FLAG_PERSISTENCE);

            switch (creditPushMsgBody.getType()){
                case CreditPushConstant.PASS_AUTH_TYPE :
                    handlePassAuthType(creditPushMsgBody);
                    break;
                case CreditPushConstant.NOT_PASS_AUTH_TYPE :
                    handleNotPassAuthType(creditPushMsgBody);
                    break;
                case CreditPushConstant.PASS_AUDIT_TYPE :
                    handlePassAuditType(creditPushMsgBody);
                    break;
                case CreditPushConstant.NOT_PASS_AUDIT_TYPE :
                    handleNotPassAuditType(creditPushMsgBody);
                    break;
                case CreditPushConstant.LEND_PASS_TYPE :
                    handleLendPassType(creditPushMsgBody);
                    break;
                case CreditPushConstant.NOT_LEND_PASS_TYPE :
                    handleNotLendPassType(creditPushMsgBody);
                    break;
                case CreditPushConstant.NOT_OVERDU_REMIND_TYPE :
                    handleNotOverduRemindType(creditPushMsgBody);
                    break;
                case CreditPushConstant.OVERDU_REMIND_TYPE :
                    handleOverduRemindType(creditPushMsgBody);
                    break;
                case CreditPushConstant.REPAY_TYPE :
                    handleRepayType(creditPushMsgBody);
                    break;
                case CreditPushConstant.REMIND_REPAY_TYPE :
                    handleRemindRepayType(creditPushMsgBody);
                    break;
                case CreditPushConstant.REPAY_SUCCESS_TYPE :
                    handleRepaySuccessType(creditPushMsgBody);
                    break;
                case CreditPushConstant.REPAY_FAIL_TYPE :
                    handleRepayFailType(creditPushMsgBody);
                    break;
                default :
                    break;
            }
        }
    }

    @Override
    public void afterPropertiesSet() throws Exception {
    }
}
